#/usr/bin/python
import os,sys,yaml
from ivic.core import Logging
from ivic.core import Utils
from ivic.core import Config
from ivic.core import project_path
from ivic.core.Config import * 

logger = Logging.get_logger('ivic.vprocess.Config')
vm_yaml = project_path('/etc/ivic/vm.yaml')



       
def get_vm_uuid():
    try:
        conf = yaml.load(file(vm_yaml))
        return conf['uuid']
    except Exception, e:
        logger.error("error at /ivic/vsprocess:get_vm_uuid-> %s" % e)

class UnionConfiguration(object):
    def __init__(self,union): 
        self.config = union       
        self.common = union['common']
        self.root = union['root']
        self.ro = union['ro'].split(',')
        self.rw = union['rw'].split(',')
        self.bind = union['bind'].split(',')
        self.nfs = union['nfs-server-space']
        self.mount_type = union['mount_type']
        self.query_addr = union['query_addr']
        
        self.option = {}
        for each in self.ro: self.option[each] = 'ro'
        for each in self.rw: self.option[each] = 'rw'
        for each in self.bind: self.option[each] = 'bind'
            
    def config_map(self):
        return self.config
    

class TransferConfiguration(object):   
    def __init__(self,transfer):
        
        self.urlprefix = transfer['urlprefix']
        self.metaFile  = transfer['urlprefix'] + "/metaFile/"
        self.warehouse = transfer['warehouse']
        self.tmproot   = transfer['tmproot']
        self.log = transfer['log']
        
        if not os.path.exists(self.log): 
            log = open(self.log,'w')
            log.close()
            
        self.config = transfer
        
    def config_map(self):
        return self.config

class RuntimeConfiguration(object):
    def __init__(self,runtime):
        
        self.root = runtime['root']
        self.preload = runtime['preload']
        self.config = runtime
        
        self.log = runtime['log']
        if not os.path.exists(self.log): 
            log = open(self.log,'w')
            log.close()
            
    def config_map(self):
        return self.config


vproc_yaml = '/etc/ivic/vprocess.yaml'
class VProcessConfig(object):
    
    def __init__(self,configmap = None):
        if configmap == None:
            config_vprocess_path = project_path(vproc_yaml)
            self.config = load(config_vprocess_path)
        else: self.config = configmap
        
        self.log = self.config['log_file']
        if not os.path.exists(self.log): 
            log = open(self.log,'w')
            log.close()
            
        self.union = UnionConfiguration(self.config['union'])
        self.transfer = TransferConfiguration(self.config['transfer'])
        self.runtime = RuntimeConfiguration(self.config['runtime'])
        self.portal_url = self.config['monitor']['url']
        self.uuid= get_vm_uuid()   

    def config_map(self):
        return self.config
        
vprocess_config = VProcessConfig()
    
if __name__ == "__main__":
    
    #make sure add logdir and log File created and chmod 0777
    #just For local test.
    logdir = '/var/log/ivic/'
    log = '/var/log/ivic/config.log'

    sys.path.insert(0, os.getcwd())
    
    from ivic.core.Config import *
    ivic.core.set_rundebug()
        
    #config_vprocess_path = project_path('/etc/ivic/vprocess.yaml')
    #config = load(config_vprocess_path)
    config = VProcessConfig()
    
    print config.union.option
